home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / ZIP_1_0.lha / src / object.c < prev    next >
C/C++ Source or Header  |  1992-10-13  |  8KB  |  416 lines

  1. /*
  2.  * object.c
  3.  *
  4.  * Object manipulation routines.
  5.  *
  6.  * Mark Howell 28-Jul-1992 V1.0
  7.  *
  8.  */
  9.  
  10. #include "ztypes.h"
  11.  
  12. #define PARENT 0
  13. #define NEXT 1
  14. #define CHILD 2
  15.  
  16. #ifdef __STDC__
  17. static zword_t read_object (zword_t objp, int field);
  18. static void write_object (zword_t objp, int field, zword_t value);
  19. #else
  20. static zword_t read_object ();
  21. static void write_object ();
  22. #endif
  23.  
  24. /*
  25.  * get_object_address
  26.  *
  27.  * Calculate the address of an object in the data area.
  28.  *
  29.  */
  30.  
  31. #ifdef __STDC__
  32. zword_t get_object_address (zword_t obj)
  33. #else
  34. zword_t get_object_address (obj)
  35. zword_t obj;
  36. #endif
  37. {
  38.     int offset;
  39.  
  40.     /* Address calculation is object table base + size of default properties area +
  41.        object number-1 * object size */
  42.  
  43.     if (h_type == V3)
  44.         offset = h_objects_offset + ((P3_MAX_PROPERTIES - 1) * 2) + ((obj - 1) * O3_SIZE);
  45.     else
  46.         offset = h_objects_offset + ((P4_MAX_PROPERTIES - 1) * 2) + ((obj - 1) * O4_SIZE);
  47.  
  48.     return ((zword_t) offset);
  49.  
  50. }/* get_object_address */
  51.  
  52. /*
  53.  * insert_object
  54.  *
  55.  * Insert object 1 as the child of object 2 after first removing it from its
  56.  * previous parent. The object is inserted at the front of the child object
  57.  * chain.
  58.  *
  59.  */
  60.  
  61. #ifdef __STDC__
  62. void insert_object (zword_t obj1, zword_t obj2)
  63. #else
  64. void insert_object (obj1, obj2)
  65. zword_t obj1;
  66. zword_t obj2;
  67. #endif
  68. {
  69.     zword_t obj1p, obj2p, child2;
  70.  
  71.     /* Get addresses of both objects */
  72.  
  73.     obj1p = get_object_address (obj1);
  74.     obj2p = get_object_address (obj2);
  75.  
  76.     /* Remove object 1 from current parent */
  77.  
  78.     remove_object (obj1);
  79.  
  80.     /* Make object 2 object 1's parent */
  81.  
  82.     write_object (obj1p, PARENT, obj2);
  83.  
  84.     /* Get current first child of object 2 */
  85.  
  86.     child2 = read_object (obj2p, CHILD);
  87.  
  88.     /* Make object 1 first child of object 2 */
  89.  
  90.     write_object (obj2p, CHILD, obj1);
  91.  
  92.     /* If object 2 had children then link them into the next child field of object 1 */
  93.  
  94.     if (child2)
  95.         write_object (obj1p, NEXT, child2);
  96.  
  97. }/* insert_object */
  98.  
  99. /*
  100.  * remove_object
  101.  *
  102.  * Remove an object by unlinking from the its parent object and from its
  103.  * siblings.
  104.  *
  105.  */
  106.  
  107. #ifdef __STDC__
  108. void remove_object (zword_t obj)
  109. #else
  110. void remove_object (obj)
  111. zword_t obj;
  112. #endif
  113. {
  114.     zword_t objp, parentp, childp, parent, child;
  115.  
  116.     /* Get address of object to be removed */
  117.  
  118.     objp = get_object_address (obj);
  119.  
  120.     /* Get parent of object, and return if no parent */
  121.  
  122.     if ((parent = read_object (objp, PARENT)) == 0)
  123.         return;
  124.  
  125.     /* Get address of parent object */
  126.  
  127.     parentp = get_object_address (parent);
  128.  
  129.     /* Find first child of parent */
  130.  
  131.     child = read_object (parentp, CHILD);
  132.  
  133.     /* If object is first child then just make the parent child pointer
  134.        equal to the next child */
  135.  
  136.     if (child == obj)
  137.         write_object (parentp, CHILD, read_object (objp, NEXT));
  138.     else {
  139.  
  140.         /* Walk down the child chain looking for this object */
  141.  
  142.         do {
  143.             childp = get_object_address (child);
  144.             child = read_object (childp, NEXT);
  145.         } while (child != obj);
  146.  
  147.         /* Set the next pointer thre previous child to the next pointer
  148.            of the current object child pointer */
  149.  
  150.         write_object (childp, NEXT, read_object (objp, NEXT));
  151.     }
  152.  
  153.     /* Set the parent and next child pointers to NULL */
  154.  
  155.     write_object (objp, PARENT, 0);
  156.     write_object (objp, NEXT, 0);
  157.  
  158. }/* remove_object */
  159.  
  160. /*
  161.  * load_parent_object
  162.  *
  163.  * Load the parent object pointer of an object
  164.  *
  165.  */
  166.  
  167. #ifdef __STDC__
  168. void load_parent_object (zword_t obj)
  169. #else
  170. void load_parent_object (obj)
  171. zword_t obj;
  172. #endif
  173. {
  174.                                        
  175.     store_operand (read_object (get_object_address (obj), PARENT));
  176.  
  177. }/* load_parent_object */
  178.  
  179. /*
  180.  * load_child_object
  181.  *
  182.  * Load the child object pointer of an object and jump if the child pointer is
  183.  * not NULL.
  184.  *
  185.  */
  186.  
  187. #ifdef __STDC__
  188. void load_child_object (zword_t obj)
  189. #else
  190. void load_child_object (obj)
  191. zword_t obj;
  192. #endif
  193. {
  194.     zword_t child;
  195.  
  196.     child = read_object (get_object_address (obj), CHILD);
  197.  
  198.     store_operand (child);
  199.  
  200.     conditional_jump (child != 0);
  201.  
  202. }/* load_child_object */
  203.  
  204. /*
  205.  * load_next_object
  206.  *
  207.  * Load the next child object pointer of an object and jump if the next child
  208.  * pointer is not NULL.
  209.  *
  210.  */
  211.  
  212. #ifdef __STDC__
  213. void load_next_object (zword_t obj)
  214. #else
  215. void load_next_object (obj)
  216. zword_t obj;
  217. #endif
  218. {
  219.     zword_t next;
  220.  
  221.     next = read_object (get_object_address (obj), NEXT);
  222.  
  223.     store_operand (next);
  224.  
  225.     conditional_jump (next != 0);
  226.  
  227. }/* load_next_object */
  228.  
  229. /*
  230.  * compare_parent_object
  231.  *
  232.  * Jump if object 2 is the parent of object 1
  233.  *
  234.  */
  235.  
  236. #ifdef __STDC__
  237. void compare_parent_object (zword_t obj1, zword_t obj2)
  238. #else
  239. void compare_parent_object (obj1, obj2)
  240. zword_t obj1;
  241. zword_t obj2;
  242. #endif
  243. {
  244.  
  245.     conditional_jump (read_object (get_object_address (obj1), PARENT) == obj2);
  246.  
  247. }/* compare_parent_object */
  248.  
  249. /*
  250.  * test_attr
  251.  *
  252.  * Test if an attribute bit is set.
  253.  *
  254.  */
  255.  
  256. #ifdef __STDC__
  257. void test_attr (zword_t obj, zword_t bit)
  258. #else
  259. void test_attr (obj, bit)
  260. zword_t obj;
  261. zword_t bit;
  262. #endif
  263. {
  264.     zword_t objp;
  265.     zbyte_t value;
  266.  
  267.     assert (O3_ATTRIBUTES == O4_ATTRIBUTES);
  268.  
  269.     /* Get attribute address */
  270.  
  271.     objp = get_object_address (obj) + (bit >> 3);
  272.  
  273.     /* Load attribute byte */
  274.  
  275.     value = get_byte (objp);
  276.  
  277.     /* Test attribute */
  278.  
  279.     conditional_jump ((value >> (7 - (bit & 7))) & 1);
  280.  
  281. }/* test_attr */
  282.  
  283. /*
  284.  * set_attr
  285.  *
  286.  * Set an attribute bit.
  287.  *
  288.  */
  289.  
  290. #ifdef __STDC__
  291. void set_attr (zword_t obj, zword_t bit)
  292. #else
  293. void set_attr (obj, bit)
  294. zword_t obj;
  295. zword_t bit;
  296. #endif
  297. {
  298.     zword_t objp;
  299.     zbyte_t value;
  300.  
  301.     assert (O3_ATTRIBUTES == O4_ATTRIBUTES);
  302.  
  303.     /* Get attribute address */
  304.  
  305.     objp = get_object_address (obj) + (bit >> 3);
  306.  
  307.     /* Load attribute byte */
  308.  
  309.     value = get_byte (objp);
  310.  
  311.     /* Set attribute bit */
  312.  
  313.     value |= (zbyte_t) (1 << (7 - (bit & 7)));
  314.  
  315.     /* Store attribute byte */
  316.  
  317.     set_byte (objp, value);
  318.  
  319. }/* set_attr */
  320.  
  321. /*
  322.  * clear_attr
  323.  *
  324.  * Clear an attribute bit
  325.  *
  326.  */
  327.  
  328. #ifdef __STDC__
  329. void clear_attr (zword_t obj, zword_t bit)
  330. #else
  331. void clear_attr (obj, bit)
  332. zword_t obj;
  333. zword_t bit;
  334. #endif
  335. {
  336.     zword_t objp;
  337.     zbyte_t value;
  338.  
  339.     assert (O3_ATTRIBUTES == O4_ATTRIBUTES);
  340.  
  341.     /* Get attribute address */
  342.  
  343.     objp = get_object_address (obj) + (bit >> 3);
  344.  
  345.     /* Load attribute byte */
  346.  
  347.     value = get_byte (objp);
  348.  
  349.     /* Clear attribute bit */
  350.  
  351.     value &= (zbyte_t) ~(1 << (7 - (bit & 7)));
  352.  
  353.     /* Store attribute byte */
  354.  
  355.     set_byte (objp, value);
  356.  
  357. }/* clear_attr */
  358.  
  359. #ifdef __STDC__
  360. static zword_t read_object (zword_t objp, int field)
  361. #else
  362. static zword_t read_object (objp, field)
  363. zword_t objp;
  364. int field;
  365. #endif
  366. {
  367.     zword_t value;
  368.  
  369.     if (h_type == V3) {
  370.         if (field == PARENT)
  371.             value = (zword_t) get_byte (PARENT3 (objp));
  372.         else if (field == NEXT)
  373.             value = (zword_t) get_byte (NEXT3 (objp));
  374.         else
  375.             value = (zword_t) get_byte (CHILD3 (objp));
  376.     } else {
  377.         if (field == PARENT)
  378.             value = get_word (PARENT4 (objp));
  379.         else if (field == NEXT)
  380.             value = get_word (NEXT4 (objp));
  381.         else
  382.             value = get_word (CHILD4 (objp));
  383.     }
  384.  
  385.     return (value);
  386.  
  387. }/* read_object */
  388.  
  389. #ifdef __STDC__
  390. static void write_object (zword_t objp, int field, zword_t value)
  391. #else
  392. static void write_object (objp, field, value)
  393. zword_t objp;
  394. int field;
  395. zword_t value;
  396. #endif
  397. {
  398.  
  399.     if (h_type == V3) {
  400.         if (field == PARENT)
  401.             set_byte (PARENT3 (objp), value);
  402.         else if (field == NEXT)
  403.             set_byte (NEXT3 (objp), value);
  404.         else
  405.             set_byte (CHILD3 (objp), value);
  406.     } else {
  407.         if (field == PARENT)
  408.             set_word (PARENT4 (objp), value);
  409.         else if (field == NEXT)
  410.             set_word (NEXT4 (objp), value);
  411.         else
  412.             set_word (CHILD4 (objp), value);
  413.     }
  414.  
  415. }/* write_object */
  416.